;(function ($) {
	var G = $.gozap, D = G.duolaidian, i18n = D.i18n;
	/*
	 * 广告展示
	 */
	function ADShow () {
		this.today = new Date();
		this.ads = [
			{'img' : '/affixes/images/show/1.jpg', src : '/shop_75895', target : '_blank',alt : '点击立即点菜', width : '240', height : '160'},
			{'img' : '/affixes/images/show/2.jpg', src : '/shop_75848', target : '_blank',alt : '点击立即点菜', width : '240', height : '160'},
			{'img' : '/affixes/images/show/3.jpg', src : '/shop_75812', target : '_blank',alt : '点击立即点菜', width : '240', height : '160'},
			{'img' : '/affixes/images/show/4.jpg', src : '/shop_75899', target : '_blank',alt : '点击立即点菜', width : '240', height : '160'},
			{'img' : '/affixes/images/show/5.jpg', src : '/shop_75895', target : '_blank',alt : '点击立即点菜', width : '240', height : '160'}
		];
		this.template = '<a target="_blank" href="${src}" ><img src="${img}" width="${width}" height="${height}"/></a>';
		this.navCount = this.ads.length;
		this.curIdx = 0;
		this.$adBox = $('.home-show div.slideshow');
		this.$adlist;
		this.$navlist = $('.home-show ul.nav');
		this.timmer = null;
	}
	
	ADShow.prototype = {
		init : function () {
			var self = this;
			self.initADList();
			self.initNavList();
			self.timmer = self.loopAnimation();
		},
		loopAnimation : function () {
			var self = this;
			if (self.timmer) {
				clearInterval(self.timmer);
				//直接将当前动画结束
				self.$adBox.find('ul').stop(true, true);
			}
			return setInterval(function () {
				var $lis = self.$navlist.find('li'),
					$li = $lis.eq(self.curIdx + 1 >= self.navCount ? 0 : self.curIdx + 1);
				$lis.removeClass('active-slide');
				$li.addClass('active-slide');
				var lastIdx = self.curIdx;
				self.curIdx = $lis.index($li);
				self.$adBox.slidebox('select', self.curIdx);
				
				var delta = self.curIdx - lastIdx,
					opr = delta < 0 ? '+=' : '-=';
				if (self.curidx == 0) {
					self.$adBox.find('ul').animate({
						'marginLeft' : 0
					}, {queue : true, duration: 400}, function () {
						//开启定时器
						self.timmer = self.loopAnimation();
					});
				} else {
					self.$adBox.find('ul').animate({
						'marginLeft' : opr + Math.abs(delta) * self.$adBox.find('li').eq(0).outerWidth(true)
					}, {queue : true, duration: 400}, function () {
						//开启定时器
						self.timmer = self.loopAnimation();
					});
				}
				
				
			}, 2000);
		},
		initADList : function () {
			var self = this;
			self.$adBox.slidebox({
				data : self.ads,
				selected : self.curIdx,
				template : self.template,
				nextBtnId : 'next',
				prevBtnId : 'prev'
			});
		},
		initNavList : function () {
			var self = this,
				tmp = [];
			for (var i = 0; i < self.navCount; i++) {
				tmp.push('<li class="' + (i == self.curIdx ? 'active-slide' : '') + '"><a href="javascript:void (0);">' + (i+1) + '</a></li>');
			}
			self.$navlist.append(tmp.join(''));
			var $lis = self.$navlist.find('li');
			$lis.bind({
				click : function (e) {
					//停止定时器
					clearInterval(self.timmer);
					//直接将当前动画结束
					self.$adBox.find('ul').stop(true, true);
					//点击操作
					$lis.removeClass('active-slide');
					$(this).addClass('active-slide');
					var lastIdx = self.curIdx;
					self.curIdx = $lis.index($(this));
					self.$adBox.slidebox('select', self.curIdx);
					var delta = self.curIdx - lastIdx,
						opr = delta < 0 ? '+=' : '-=';
					self.$adBox.find('ul').animate({
						'marginLeft' : opr + Math.abs(delta) * self.$adBox.find('li').eq(0).outerWidth(true)
					}, {queue : true, duration: 400}, function () {
						//开启定时器
						//self.timmer = self.loopAnimation();
					});
					setTimeout(function () {
						self.timmer = self.loopAnimation();
					}, 400);
					
				}
			});
			$lis.find('a').hover(function (e) {
				$(this).addClass('hover');
			}, function (e) {
				$(this).removeClass('hover');
			});
		}
	};
	$(document).ready(function () {
		var adshow = new ADShow().init();
	});
	
})(jQuery);